package com.fs.mapper;

import com.fs.entity.Order;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface OrderMapper {

    @Insert("INSERT INTO orders (order_no, user_id, total_amount, pay_amount, discount_amount, payment_method, status, order_time, receiver_name, receiver_phone, receiver_address, remark, create_time, update_time, deleted) " +
            "VALUES (#{orderNo}, #{userId}, #{totalAmount}, #{payAmount}, #{discountAmount}, #{paymentMethod}, #{status}, #{orderTime}, #{receiverName}, #{receiverPhone}, #{receiverAddress}, #{remark}, #{createTime}, #{updateTime}, #{deleted})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);

    @Select("<script>" +
            "SELECT * FROM orders WHERE user_id = #{userId} AND deleted = 0 " +
            "<if test='status != null'> AND status = #{status} </if>" +
            " ORDER BY order_time DESC" +
            "</script>")
    List<Order> selectByUserIdAndStatus(@Param("userId") Long userId, @Param("status") Integer status);

    @Select("SELECT * FROM orders WHERE id = #{id} AND deleted = 0")
    Order selectById(Long id);

    @Update("UPDATE orders SET status = #{status}, payment_time = NOW(), update_time = NOW() WHERE id = #{id}")
    int payOrder(@Param("id") Long id, @Param("status") Integer status);

    @Update("UPDATE orders SET status = #{status}, cancel_time = NOW(), update_time = NOW() WHERE id = #{id}")
    int cancelOrder(@Param("id") Long id, @Param("status") Integer status);

    @Update("UPDATE orders SET status = #{status}, receive_time = NOW(), update_time = NOW() WHERE id = #{id}")
    int confirmOrder(@Param("id") Long id, @Param("status") Integer status);
}
